﻿using System;
using NUnit.Framework;
using ValidationFramework.Specifications.Tests.Helpers;
using ValidationFramework.Specifications;

namespace ValidationFramework.Tests.Configuration.ConfigReaders
{
    [TestFixture]
    public class IsInRangeParserTests
    {
        [Test]
        public void Should_Infer_Generic_Type_From_InfoDescriptor()
        {
            // Arrange
            const string validatorXml = "<rule minimum='1' equalsMinimumIsValid='false' equalsMaximumIsValid='false' maximum='5'/>";
            var element = validatorXml.ToXmlElement();
            var configReader = new IsInRangeParser();

            // Act
            var validator = configReader.GetSpecification(element, FakeRuleContext.GetContext(typeof(int)));

            // Assert
            Assert.IsInstanceOfType(typeof(IsInRangeSpec<int>), validator);
        }
        [Test]
        public void Should_Parse_Non_String_Types()
        {
            const string validatorXml = "<rule minimum='10 Mar 2007' maximum='12 Mar 2007'/>";
            var element = validatorXml.ToXmlElement();
            var configReader = new IsInRangeParser();

            // Act
            var validator = (IsInRangeSpec<DateTime>)configReader.GetSpecification(element, FakeRuleContext.GetContext(typeof(DateTime)));

            // Assert
            Assert.IsInstanceOfType(typeof(IsInRangeSpec<DateTime>), validator);
            Assert.AreEqual(new DateTime(2007, 3, 10), validator.Minimum);
            Assert.AreEqual(new DateTime(2007, 3, 12), validator.Maximum);
        }
        [Test]
        public void Can_Read_Validato_Xml_Element()
        {
            // Arrange
            const string validatorXml = "<rule minimum='1' equalsMinimumIsValid='false' equalsMaximumIsValid='false' maximum='5'/>";
            var element = validatorXml.ToXmlElement();

            var configReader = new IsInRangeParser();

            // Act
			var validator = (IsInRangeSpec<int>)configReader.GetSpecification(element, FakeRuleContext.GetContext());

            // Assert
            Assert.AreEqual(1, validator.Minimum);
            Assert.AreEqual(5, validator.Maximum);
            Assert.IsFalse(validator.EqualsMinimumIsValid);
            Assert.IsFalse(validator.EqualsMaximumIsValid);
        }

        //[Test]
        //public void ReadConfigNullable()
        //{
        //    const string validatorXml = "<rule errorMessage='hello' typeName='RangeRule' minimum='1' ruleSet='foo' equalsMinimumIsValid='false' equalsMaximumIsValid='false' maximum='5'/>";
        //    var xmlSerializer = new XmlSerializer(typeof(RuleData));
        //    var ruleData = (RuleData)xmlSerializer.Deserialize(new StringReader(validatorXml));
        //    var rule = (RangeRule<int>)ConfigurationService.GetRule(ruleData, typeof(int?).TypeHandle);
        //    Assert.AreEqual("hello", rule.ErrorMessage);
        //    Assert.AreEqual(1, rule.Minimum);
        //    Assert.AreEqual(5, rule.Maximum);
        //    Assert.AreEqual("FOO", rule.RuleSet);
        //    Assert.IsFalse(rule.EqualsMinimumIsValid);
        //    Assert.IsFalse(rule.EqualsMaximumIsValid);
        //}

        //[Test]
        //public void ReadDateTimeConfig()
        //{
        //    const string validatorXml = "<rule errorMessage='hello' typeName='RangeRule' minimum='10 Mar 2007' ruleSet='foo' equalsMinimumIsValid='false' equalsMaximumIsValid='false' maximum='12 Mar 2007'/>";
        //    var xmlSerializer = new XmlSerializer(typeof(RuleData));
        //    var ruleData = (RuleData)xmlSerializer.Deserialize(new StringReader(validatorXml));
        //    var rule = (RangeRule<DateTime>)ConfigurationService.GetRule(ruleData, typeof(DateTime).TypeHandle);
        //    Assert.AreEqual("hello", rule.ErrorMessage);
        //    Assert.AreEqual(new DateTime(2007, 3, 10), rule.Minimum);
        //    Assert.AreEqual(new DateTime(2007, 3, 12), rule.Maximum);
        //    Assert.AreEqual("FOO", rule.RuleSet);
        //    Assert.IsFalse(rule.EqualsMinimumIsValid);
        //    Assert.IsFalse(rule.EqualsMaximumIsValid);
        //}

        //[Test]
        //public void ReadDateTimeConfigNullable()
        //{
        //    const string validatorXml = "<rule errorMessage='hello' typeName='RangeRule' minimum='10 Mar 2007' ruleSet='foo' equalsMinimumIsValid='false' equalsMaximumIsValid='false' maximum='12 Mar 2007'/>";
        //    var xmlSerializer = new XmlSerializer(typeof(RuleData));
        //    var ruleData = (RuleData)xmlSerializer.Deserialize(new StringReader(validatorXml));
        //    var rule = (RangeRule<DateTime>)ConfigurationService.GetRule(ruleData, typeof(DateTime?).TypeHandle);
        //    Assert.AreEqual("hello", rule.ErrorMessage);
        //    Assert.AreEqual(new DateTime(2007, 3, 10), rule.Minimum);
        //    Assert.AreEqual(new DateTime(2007, 3, 12), rule.Maximum);
        //    Assert.AreEqual("FOO", rule.RuleSet);
        //    Assert.IsFalse(rule.EqualsMinimumIsValid);
        //    Assert.IsFalse(rule.EqualsMaximumIsValid);
        //}

        //[Test]
        //public void ReadConfig()
        //{
        //    const string validatorXml = "<rule errorMessage='hello' typeName='RangeStringRule' ruleSet='foo' minimum='a' maximum='c' equalsMinimumIsValid='false' equalsMaximumIsValid='false' />";
        //    var xmlSerializer = new XmlSerializer(typeof(RuleData));
        //    var ruleData = (RuleData)xmlSerializer.Deserialize(new StringReader(validatorXml));
        //    var rule = (RangeStringRule)ConfigurationService.GetRule(ruleData, TypePointers.StringTypeHandle);
        //    Assert.AreEqual("hello", rule.ErrorMessage);
        //    Assert.AreEqual("a", rule.Minimum);
        //    Assert.AreEqual("c", rule.Maximum);
        //    Assert.AreEqual("FOO", rule.RuleSet);
        //    Assert.IsFalse(rule.EqualsMinimumIsValid);
        //    Assert.IsFalse(rule.EqualsMaximumIsValid);



        //}
    }
}